<template>
  <div>
    <el-row :gutter="24">
      <el-col :span="4">
        <el-card class="box-card">
          <el-tree
            ref="treeBox"
            :data="catgoretyTree"
            :props="defaultProps"
            :expand-on-click-node="false"
            :highlight-current="true"
            node-key="departmentId"
            @node-click="handleNodeClick"
          />
        </el-card>
      </el-col>
      <el-col :span="20">
        <!--卡片-->
        <el-card class="box-card">
          <!-- 搜索与添加区域 -->
          <el-button v-hasPermi="['admin','product:attr:add']" type="primary" small class="addBtn" style="margin:10px" size="mini" icon="el-icon-plus" @click="addBtn()">新增
          </el-button>
          <!--表格-->
          <el-table :data="listForm" stripe border style="width: 100%">
            <el-table-column prop="attrId" header-align="center" align="center" label="属性id" />
            <el-table-column prop="attrName" header-align="center" align="center" label="属性名" />
            <el-table-column prop="searchType" header-align="center" align="center" label="是否检索">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.searchType" size="mini" type="primary">是</el-tag>
                <el-tag v-else size="mini" type="success">否</el-tag>
              </template>
            </el-table-column>

            <el-table-column prop="valueType" header-align="center" align="center" label="值类型">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.searchType" size="mini" type="primary">多选</el-tag>
                <el-tag v-else size="mini" type="success">单选</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="icon" header-align="center" align="center" label="属性图标" />
            <el-table-column :show-overflow-tooltip="true" prop="valueSelect" header-align="center" align="center" label="可选值列表">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.valueSelect===''" size="mini">——</el-tag>
                <el-tag v-else size="mini">{{ scope.row.valueSelect }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="attrType" header-align="center" align="center" label="属性类型">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.attrType" size="mini" type="primary">基本属性</el-tag>
                <el-tag v-else size="mini" type="success">销售属性</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="enable" header-align="center" align="center" label="启用状态">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.enable" size="mini" type="success">是</el-tag>
                <el-tag v-else size="mini" type="danger">否</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="catelogName" header-align="center" align="center" label="所属分类" />
            <el-table-column prop="attrGroupName" header-align="center" align="center" label="所属分组" />
            <el-table-column prop="showDesc" header-align="center" align="center" label="快速展示">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.showDesc" size="mini" type="success">是</el-tag>
                <el-tag v-else size="mini" type="danger">否</el-tag>
              </template>
            </el-table-column>
            <!--操作-->
            <el-table-column label="操作" fixed="right" width="130px">
              <template slot-scope="scope">
                <el-button v-hasPermi="['admin','product:attr:edit']" type="text" size="mini" @click="editBtn(scope.row.attrId)">编辑</el-button>
                <el-button v-hasPermi="['admin','product:attr:del']" type="text" size="mini" @click="delBtn(scope.row.attrId)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!--分页区域-->
          <el-pagination
            background
            :current-page="pageNumber"
            :page-sizes="[1, 2, 5, 10]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-card>
      </el-col>
    </el-row>
    <!--添加对话框-->
    <el-dialog title="添加" :close-on-click-modal="false" :visible.sync="addDialogVisible" width="580px" @close="addHandleClose">
      <span>
        <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="100px">
          <el-form-item label="属性名" prop="attrName">
            <el-input v-model="addForm.attrName" />
          </el-form-item>

          <el-form-item label="属性类型" prop="attrType">
            <el-select v-model="addForm.attrType" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="值类型" prop="valueType">
            <el-switch
              v-model="addForm.valueType"
              active-text="允许多个值"
              inactive-text="只能单个值"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
          <el-form-item label="可选值" prop="valueSelect">
            <el-select
              v-model="valueSelectList"
              multiple
              filterable
              allow-create
              default-first-option
              placeholder="请输入内容"
            >
              <el-option
                v-for="item in options2"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="属性图标" prop="icon">
            <el-input v-model="addForm.icon" />
          </el-form-item>

          <el-form-item label="所属分类" prop="catelogIds">
            <el-cascader
              v-model="addForm.catelogIds"
              placeholder="试试搜索：手机"
              filterable
              :options="catgoretyTree"
              :props="defaultProps"
              style="width: 450px"
              @change="handleChange"
            />
          </el-form-item>
          <el-form-item label="所属分组" prop="attrGroupId">
            <el-select v-model="addForm.attrGroupId" placeholder="请选择">
              <el-option
                v-for="item in attrGroupIds"
                :key="item.attrGroupId"
                :label="item.attrGroupName"
                :value="item.attrGroupId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="启用状态" prop="enable">
            <el-switch
              v-model="addForm.enable"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
          <el-form-item label="是否检索" prop="searchType">
            <el-switch
              v-model="addForm.searchType"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
          <el-form-item label="快速展示" prop="showDesc">
            <el-switch
              v-model="addForm.showDesc"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClick">确 定</el-button>
      </span>
    </el-dialog>

    <!--编辑对话框-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialogVisible" width="580px" @close="editHandleClose">
      <span>
        <el-form ref="editFormRef" :model="editForm" label-width="100px">
          <el-form-item label="属性名" prop="attrName">
            <el-input v-model="editForm.attrName" />
          </el-form-item>

          <el-form-item label="属性类型" prop="attrType">
            <el-select v-model="editForm.attrType" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="值类型" prop="valueType">
            <el-switch
              v-model="editForm.valueType"
              active-text="允许多个值"
              inactive-text="只能单个值"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
          <el-form-item label="可选值" prop="valueSelect">
            <el-select
              v-model="valueSelectList"
              multiple
              filterable
              allow-create
              default-first-option
              placeholder="请输入内容"
            >
              <el-option
                v-for="item in options2"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="属性图标" prop="icon">
            <el-input v-model="editForm.icon" />
          </el-form-item>

          <el-form-item label="所属分类" prop="catelogIds">
            <el-cascader
              v-model="editForm.catelogIds"
              placeholder="试试搜索：手机"
              filterable
              :options="catgoretyTree"
              :props="defaultProps"
              style="width: 450px"
              @change="handleChange"
            />
          </el-form-item>
          <el-form-item label="所属分组" prop="attrGroupId">
            <el-select v-model="editForm.attrGroupId" placeholder="请选择">
              <el-option
                v-for="item in attrGroupIds"
                :key="item.attrGroupId"
                :label="item.attrGroupName"
                :value="item.attrGroupId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="启用状态" prop="enable">
            <el-switch
              v-model="editForm.enable"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
          <el-form-item label="是否检索" prop="searchType">
            <el-switch
              v-model="editForm.searchType"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
          <el-form-item label="快速展示" prop="showDesc">
            <el-switch
              v-model="editForm.showDesc"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClick">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { tree } from '@/api/product/category'
import { add, edit, del, getAttr, listAttr } from '@/api/product/attr'
import { listAttrGroupAll } from '@/api/product/attrgroup'
export default {
  name: 'Baseattr',
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      pageNumber: 1,
      pageSize: 10,
      total: 0,
      currentCatelogId: 0,
      currentCttrType: 'base',
      catgoretyTree: [],
      defaultProps: {
        children: 'children',
        label: 'name',
        value: 'catId'
      },
      options: [{
        value: 0,
        label: '销售属性'
      }, {
        value: 1,
        label: '基本属性'
      }
      ],
      options2: [],
      attrGroupIds: [],
      listForm: [],
      valueSelectList: [],
      addForm: {
        attrGroupId: '',
        attrId: '',
        attrName: '',
        searchType: '',
        valueType: '',
        icon: '',
        valueSelect: '',
        attrType: 1,
        enable: 1,
        catelogId: '',
        catelogIds: [],
        showDesc: ''
      },
      editForm: {},
      addFormRules: {
        attrName: [
          { required: true, message: '请输入属性名', trigger: 'blur' }
        ],
        attrGroupId: [
          { required: true, message: '请输入分组', trigger: 'blur' }
        ],
        catelogIds: [
          { required: true, message: '请输入分类', trigger: 'blur' }
        ],
        attrType: [
          { required: true, message: '请输入属性类型', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.initListCatgorety()
  },

  methods: {
    // 选择分类
    handleChange(value) {
      this.currentCatelogId = value[value.length - 1]
      this.initListAttrGroupAll()
    },
    // 分类点击事件
    handleNodeClick(data) {
      this.currentCatelogId = data.catId
      this.listForm = []
      if (data.catLevel === 3) {
        this.initListtAttr()
      }
    },
    // 初始化分类数据
    initListCatgorety() {
      tree().then(res => {
        this.catgoretyTree = res.data
      })
    },

    // 添加按钮
    addBtn() {
      this.addDialogVisible = true
    },

    // 确认添加
    addClick() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        this.addForm.catelogId = this.addForm.catelogIds[this.addForm.catelogIds.length - 1]

        for (let i = 0; i < this.valueSelectList.length; i++) {
          if (i === 0) {
            this.addForm.valueSelect = this.valueSelectList[i]
          } else {
            this.addForm.valueSelect = this.addForm.valueSelect + ',' + this.valueSelectList[i]
          }
        }
        add(this.addForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.currentCatelogId = this.addForm.catelogId
            this.initListtAttr()
            // 关闭对话框
            this.addDialogVisible = false
          }
        })
      })
    },

    // 编辑按钮
    async  editBtn(id) {
      this.editDialogVisible = true
      await getAttr(id).then(res => {
        if (res.code === 20000) {
          this.editForm = res.data
          this.valueSelectList = this.editForm.valueSelect.split(';')
        }
      })
      this.currentCatelogId = this.editForm.catelogId
      await this.initListAttrGroupAll()
    },

    // 确认编辑
    editClick() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        for (let i = 0; i < this.valueSelectList.length; i++) {
          if (i === 0) {
            this.editForm.valueSelect = this.valueSelectList[i]
          } else {
            this.editForm.valueSelect = this.editForm.valueSelect + ',' + this.valueSelectList[i]
          }
        }
        edit(this.editForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListtAttr()
          }
          // 关闭对话框
          this.editDialogVisible = false
        })
      })
    },

    // 删除按钮
    delBtn(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        del(id).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            this.pageNumber = 1
            // 刷新数据
            this.initListtAttr()
          }
        })
      })
    },

    // 监听当前页的事件
    handleCurrentChange(pageIndex) {
      this.pageNumber = pageIndex
      this.initListtAttr()
    },

    // 监听当前页的条数事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.initListtAttr()
    },

    // 监听添加对话框的关闭事件，重置表单内容
    addHandleClose() {
      this.$refs.addFormRef.resetFields()
      this.addForm.catelogIds = []
      this.valueSelectList = []
    },

    // 监听编辑对话框的关闭事件,重置表单内容
    editHandleClose() {
      this.$refs.editFormRef.resetFields()
      this.editForm.catelogIds = []
      this.valueSelectList = []
    },

    // 查询当前分类下的属性
    initListtAttr() {
      const params = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize,
        catelogId: this.currentCatelogId,
        type: this.currentCttrType
      }
      listAttr(params).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    },
    // 查询当前分类下的所有分组
    initListAttrGroupAll() {
      const params = {
        catelogId: this.currentCatelogId
      }
      listAttrGroupAll(params).then(res => {
        this.attrGroupIds = res.data
      })
    }

  }
}
</script>

<style scoped>
</style>
